<template>
  <div class="container">
    <div class="left">
      <div class="icon">
        <el-icon v-if="!settingStore.menu_fold" @click="settingStore.menu_fold = true" style="cursor: pointer">
          <Expand />
        </el-icon>
        <el-icon v-else @click="settingStore.menu_fold = false" style="cursor: pointer">
          <Fold />
        </el-icon>
      </div>
      <!-- 面包屑 -->
      <el-breadcrumb separator="/">
        <template v-for="item in route.matched">
          <el-breadcrumb-item v-if="item.path !== '/'" :to="{ path: item.path }">{{ item.meta.title }}</el-breadcrumb-item>
        </template>
      </el-breadcrumb>
    </div>
    <div class="right">
      <!-- 全屏 -->
      <el-button @click="fullScreen" size="small" icon="FullScreen" circle />
      <!-- 设置 -->

      <el-popover placement="top-start" title="设置" :width="200" trigger="hover">
        <template #reference>
          <el-button size="small" icon="Setting" circle />
        </template>

        <el-form>
          <el-form-item label="主题颜色">
            <el-color-picker @change="changColor" v-model="color" />
          </el-form-item>
          <el-form-item label="暗黑模式">
            <el-switch @change="changeDark" v-model="mode" />
          </el-form-item>
        </el-form>
      </el-popover>
      <!-- 用户 -->
      <el-dropdown>
        <div class="userinfo">
          <img class="avator" :src="avatar" alt="" />
          <span>{{ name }}</span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useSettingStore } from '@/store/module/setting'
import { useUserStore } from '@/store/module/user'
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()

const userStore = useUserStore()
const settingStore = useSettingStore()
const router = useRouter()

// 模式
const mode = ref<boolean>(false)
// 颜色选择器默认颜色
const color = ref('#409EFF')

const { avatar, name } = storeToRefs(userStore)
// 退出登录
const logout = async () => {
  await userStore.post_logout()
  router.push({ path: '/login', query: { redirect: route.path } })
  ElMessage.success('退出成功')
}

// 点击全屏按钮
const fullScreen = () => {
  //DOM对象的一个属性:可以用来判断当前是不是全屏模式[全屏:true,不是全屏:false]
  let full = document.fullscreenElement
  if (!full) {
    document.documentElement.requestFullscreen()
  } else {
    //变为不是全屏模式->退出全屏模式
    document.exitFullscreen()
  }
}

// 暗黑模式
const changeDark = () => {
  let html = document.documentElement
  mode.value ? (html.className = 'dark') : (html.className = '')
}

// 改变主题颜色
const changColor = () => {
  //通知js修改根节点的样式对象的属性与属性值
  const html = document.documentElement
  html.style.setProperty('--el-color-primary', color.value)
  settingStore.color = color.value
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .right {
    width: 12%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    font-size: 14px;
  }

  .left {
    display: flex;
    justify-content: center;
    align-items: center;

    .icon {
      margin-right: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .userinfo {
    display: flex;
    align-items: center;

    .avator {
      height: 30px;
      width: 30px;
      border-radius: 50%;
    }

    span {
      margin-left: 4px;
    }
  }
}
</style>
